package TUILag;
import CtrLag.*;
import ModelLag.*;

/**
  * Handles the Loan TUI in the system
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class LoanTUI
{
    private InputReader reader;
    private LoanCtr lCtr;

    /**
     * Constructor for objects of class LoanTUI
     */
    public LoanTUI()
    {
        lCtr = new LoanCtr();
        reader = InputReader.getInstance();
    }
    
    /**
     * Runs the LoanTUI 
    */
    public void run(){
        printMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    createLoan();
                    break;
                case "2":
                    findLoan();
                    break;
                case "3":
                    endLoan();
                    break;
                case "4":
                    finished = true;
                    break;
                default:
                    System.out.println("Not a command");
                    break;
            }
            printMenu();
        }
    }
    
     /**
     * Prints the Loan menu to the terminal
     */
    private void printMenu(){
        System.out.println("** Loan **");
        System.out.println("1. Create new loan");
        System.out.println("2. Find loan");
        System.out.println("3. End loan");
        System.out.println("4. Main menu");
    }
    
    /**
     * Create a new loan of a Copy c to a person p
     * */
    private void createLoan(){
       try{
        // get person
        String phone = reader.getInputWithLabel("Friend phonenumber");
        Person p = lCtr.findPerson(phone);
        //get copy
        String serialNumber = reader.getInputWithLabel("Copy serialnumber");
        Copy c = lCtr.findCopy(serialNumber);
        
        //get returndate of loan
        String returnDate = reader.getInputWithLabel("Return date (YYYY-MM-DD)");
        
        //Send menssage to controller
        lCtr.CreateLoan(p, returnDate, c);
       }catch(Exception e){
           System.out.println("Error, Copy or Person not found");
       }
    }
    
    /**
     * Prints information about a loan found from the copy´s serialnumber
     */
    private void findLoan(){
        // the serial number of the copy in the loan
        String serialNumber = reader.getInputWithLabel("Serial number");
        try{
            Loan l = lCtr.findLoan(serialNumber);
            System.out.println("Copy Serial number: " + l.GetCopy().getSerialNumber());
            System.out.println("Borrowed by: " + l.GetPerson().getName());
            System.out.println("Loan date: " + l.getLoanDate());
            System.out.println("Return date: " + l.getReturnDate());
        }catch(Exception e){
            System.out.println("Loan not found!");
        }
        
    }
    
    /**
     * Ends an active loan 
     */
    private void endLoan(){
        // the serial number of the copy in the loan
        String serialNumber = reader.getInputWithLabel("Serial number");
        
        try{
            Loan l = lCtr.findLoan(serialNumber);
            boolean ontime = lCtr.endLoan(l);
            if(ontime){
                System.out.println("The loan was returned on time!");
            }else{
                System.out.println("The loan was not returned on time!");
            }
        }catch(Exception e){
            System.out.println("Loan not found!");
        }
        
    }   
}
